package com.hspedu.map_;

import java.util.*;

/**
 * @author DL5O
 * @version 1.0
 */
public class MapExercise {
    public static void main(String[] args) {
//        HashMap<String,Employee> hashMap = new HashMap<>();
        HashMap hashMap = new HashMap();
        hashMap.put(1, new Employee("小张", 19100, 1));
        hashMap.put(2, new Employee("小宋", 19500, 2));
        hashMap.put(3, new Employee("小杨", 18000, 3));
        hashMap.put(3, new Employee("小杨", 19200, 3));
        hashMap.put(4, "大龙");

        /*//第一种方式，取出keyset 利用for循环遍历
        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object e =  iterator.next();
            Employee employee = hashMap.get(e);
            if (employee.getSalary() > 18000){
                System.out.println(employee);
                System.out.println();
            }
        }*/

       /* 第二种 使用values方法遍历
        Collection values = hashMap.values();
        for (Object o : values) {
            Employee e = (Employee) o;
            if (e.getSalary() > 18000) {
                System.out.println(e);
            }
        }*/

        //第三种使用keySet 增强for进行遍历
        /*Set keySet = hashMap.keySet();
        for (Object key : keySet) {
            Object o = hashMap.get(key);//返回的是一个value的运行类型
            //System.out.println(o.getClass());//运行类型为Employee
            if(o instanceof Employee){
                Employee e = (Employee) o;
                if(e.getSalary > 18000){
                    System.out.println(e);
                }

            }else{
                System.out.println(o);
            }
            System.out.println();
        }*/

        //第四种方式，使用entryset
        Set entrySet = hashMap.entrySet();
//        System.out.println(entrySet.getClass());//entrySet
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            //System.out.println(obj.getClass());//class java.util.HashMap$Node

            Map.Entry e = (Map.Entry) obj;
            //System.out.println(e.getClass());//class java.util.HashMap$Node

            Object value = e.getValue();
            //System.out.println(value.getClass());//class com.hspedu.map_.Employee

            if (value instanceof Employee) {
                Employee employee = (Employee) value;
                if (employee.getSalary() > 1800) {
                    System.out.println(value);
                }
            } else {
                System.out.println(value);
            }
            System.out.println();
        }


        /*
        员工id:1
        姓名:小张
        工资:19000.0

        员工id:2
        姓名:小宋
        工资:18500.0

        员工id:3
        姓名:小杨
        工资:19000.0

        大龙

         */
    }


}

class Employee {
    private String name;
    private double salary;
    private int id;

    public Employee(String name, double salary, int id) {
        this.name = name;
        this.salary = salary;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    @Override
    public String toString() {
        return "员工id:" + id +
                "\n姓名:" + name +
                "\n工资:" + salary;
    }
}